package com.sky;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @author hm
 * @package com.sky
 * @date Created in 2024/7/11 08:26
 */
public class HttpClientDemo {
    public static void main(String[] args) throws IOException, JSONException, URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("服务端返回的状态码为：" + statusCode);
        System.out.println("服务端返回的状态码为"+response.getStatusLine());
        HttpEntity entity = response.getEntity();
        System.out.println("响应实体"+entity);
        String string = EntityUtils.toString(entity);
        System.out.println("响应"+string);
        response.close();
        httpClient.close();
        //post请求
//        postHttp();
        loginUni();
    }

    public static void postHttp() throws JSONException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");
        StringEntity stringEntity = new StringEntity(jsonObject.toString(),"UTF-8");
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        System.out.println(response);
        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity);
        System.out.println("响应："+string);
        StatusLine statusLine = response.getStatusLine();
        System.out.println("状态码："+statusLine.getStatusCode());
    }
    public static void loginUni() throws JSONException, IOException, URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String code="0e3ulBll2rwILd4JJ2ll26Xdo93ulBlW";
        String uri = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=wx356616a534f40678&secret=a51dd1b553f4df73b094982e5e420174&js_code=%s&grant_type=authorization_code",code);
        HttpGet httpGet = new HttpGet(uri);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity);
        System.out.println("响应"+string);
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(string);
        System.out.println("响应对象："+jsonObject);
        String string1 = jsonObject.getString("openid");
        String string2 = jsonObject.getString("session_key");
        System.out.println("openid："+ string1);
        System.out.println("session_key："+ string2);
    }


}